//
//  WPPrivateInfoTVC.m
//  wobingwoyi
//
//  Created by WGP on 16/9/7.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPPrivateInfoTVC.h"
#import "WPPrivateInfoTableViewCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

@interface WPPrivateInfoTVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIView *noInfoView;
@property(nonatomic,strong)NSArray *models;

@end

@implementation WPPrivateInfoTVC

-(NSArray *)models
{
    if (_models == nil)
    {
        WPUserAccountViewModel *userAccountModel = [WPUserAccountViewModel sharedInstance];
        WPUserAccount *userAccount = userAccountModel.userAccount;
        NSString *privateInfomationArr = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"privateInfomationArr_%zd.plist",userAccount.user.id]];
        
        _models = [NSKeyedUnarchiver unarchiveObjectWithFile:privateInfomationArr];
        
        _models = [[_models reverseObjectEnumerator] allObjects];
        if (_models == nil)
        {
            _models = [NSMutableArray array];
        }
    }
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titleLabel.text = @"个人消息";
    
    [self noInfoView];
    [self tableView];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [WPUserAccountViewModel sharedInstance].isNewPersonalInfo = NO;
    
    if(!self.models.count)
    {
        self.tableView.hidden = YES;
    }
}

-(UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 25)];
        _tableView.backgroundColor  =GlobalBgColor;
        [_tableView registerClass:[WPPrivateInfoTableViewCell class] forCellReuseIdentifier:@"WPPrivateInfoTableViewCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.allowsSelection = NO;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

-(UIView *)noInfoView
{
    if (_noInfoView == nil)
    {
        _noInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _noInfoView.backgroundColor = GlobalBgColor;
        [self.view addSubview:_noInfoView];
        
        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"collection_record"]];
        [iconView sizeToFit];
        [_noInfoView addSubview:iconView];
        
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noInfoView);
            make.top.equalTo(_noInfoView).offset(124*K_SCALE_ELSE_TABBAR);
        }];
        
        UILabel *label = [UILabel new];
        label.text  =@"暂无个人消息";
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = GloballightFontColor;
        [_noInfoView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_noInfoView);
            make.top.equalTo(iconView.mas_bottom).offset(35*K_SCALE_ELSE_TABBAR);
        }];
        
    }
    return _noInfoView;
}

#pragma mark - tableView的数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WPPrivateInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPPrivateInfoTableViewCell"];
    cell.model = self.models[indexPath.item];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"WPPrivateInfoTableViewCell" cacheByIndexPath:indexPath configuration:^(WPPrivateInfoTableViewCell *cell) {
        // 配置 cell 的数据源，和 "cellForRow" 干的事一致，比如：
        cell.model = self.models[indexPath.row];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
